package com.lbn.monitor.app.monitor;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.free.framework.ui.FjrAppbar;
import com.lbn.monitor.R;
import com.lbn.monitor.api.model.Device;
import com.lbn.monitor.app.monitor.adapter.MfListAdapter;
import com.lbn.monitor.app.monitor.facility.FacilityDetailActivity;
import com.lbn.monitor.base.AppBaseActivity;
import com.lbn.monitor.widget.Appbar;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import in.srain.cube.views.ptr.PtrFrameLayout;
import indi.free.widgets.custom_recycler_view.BasePagerRecyclerAdapter;

public class MFListActivity extends AppBaseActivity<MFListPresenter> implements IMFListView<List<Device>>,
        AppBaseActivity.OnPtrRefresh,BasePagerRecyclerAdapter.OnLoadMoreListener,BasePagerRecyclerAdapter.OnItemClickListener{

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.ptr_frame)
    PtrFrameLayout ptrFrame;
    private MfListAdapter adapter;
//    private static final String TYPE = "TYPE";
//    private int type;

    // 0 = 实时 1 = 分钟 2 = 小时
    private int dataType;

    private static final String TYPE = "TYPE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mflist);
        ButterKnife.bind(this);
//        type = getIntent().getIntExtra(TYPE,0);
        setAppbar(new Appbar(this)
                .setTitle("监测设备列表")
                .openBack());
//        setAppbar(new FjrAppbar.Builder(this)
//                .title("设备列表")
//                .titleInCenter(true)
//                .create());
    }

    public static void startActivity(Context context,int dataType){
        Intent intent = new Intent(context,MFListActivity.class);
        intent.putExtra(TYPE,dataType);
        context.startActivity(intent);
    }


    protected MFListPresenter createPresenter(){
        return new MFListPresenter();
    }

    @Override
    protected void onPresenterCreated() {
        super.onPresenterCreated();
        dataType = getIntent().getIntExtra(TYPE,0);
        adapter = new MfListAdapter(this,recyclerView,null);
        adapter.setmOnLoadMoreListener(this);
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
        initPtr(ptrFrame,this);
    }

    int page = 1;

    @Override
    public void onPtrRefresh() {
        page = 1;
        mPresenter.list(page);
    }

    @Override
    public void onLoadMore() {
        page++;
        mPresenter.list(page);
    }

    @Override
    public void refreshComplete(List<Device> data) {
        adapter.refreshData(data,true);
        ptrFrame.refreshComplete();
    }

    @Override
    public void addMoreData(List<Device> data, boolean isMore) {
        adapter.addMoreData(data,isMore);
    }

    @Override
    public void onItemClick(View itemView, int pos) {
        Device data = adapter.getItem(pos);
        int type = 0;
        try {
            type = Integer.parseInt(data.getDeviceType());
        }catch (Exception e){
            if(data.getInstallLocation().contains("原烟")){
                type = 1;
            }else{
                type = 2;
            }
        }
        String deviceMN = data.getDeviceMN();
        String deviceId = data.getId();
        FacilityDetailActivity.startActivity(this,data.getInstallLocation(),type,deviceMN,deviceId,dataType);
    }
}
